﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ES=Entity.SingleChain;
using En=Entity;

namespace Bussiness.SingleChain
{
    public class Sort
    {
        /// <summary>
        /// 插入排序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static En.ExecultResult<En.SortResult> insertSort(ES.Node list)
        {
            if (list == null || BaseOperate.Length(list) < 2)
            {
                return new En.ExecultResult<En.SortResult>()
                {
                    OutCode = -1,
                    OutMessage = "参数错误",
                };
            }

            DateTime startTime = System.DateTime.Now;
            int loopCount = 0;
            int exchangeCount = 0;
            ES.Node p = list.Next;
            while (p.Next != null)
            {

                ES.Node s = list;
                while (s != p)
                {
                    loopCount++;
                    if (s.Next.Num > p.Next.Num)
                    {
                        exchangeCount++;
                        ES.Node t = p.Next;
                        p.Next = t.Next;
                        t.Next = s.Next;
                        s.Next = t;
                        break;
                    }
                    s = s.Next;
                }
                if (s == p)
                {
                    p = p.Next;
                }
            }
            DateTime endTime = System.DateTime.Now;
            string strDuration = ((endTime - startTime).TotalMilliseconds).ToString();
            return new En.ExecultResult<En.SortResult>()
            {
                OutCode = 1,
                OutMessage = "成功",
                Data = new En.SortResult()
                {
                    LoopCount = loopCount,
                    ExChangCount = exchangeCount,
                    Duration = strDuration,
                    StartT = startTime.ToString("HH:mm:ss ffff"),
                    EndT = endTime.ToString("HH:mm:ss ffff")
                }
            };

        }

        public static En.ExecultResult<En.SortResult> simpleInsertSort(ES.Node list)
        {
            if (BaseOperate.Length(list) < 2)
            {
                return new En.ExecultResult<En.SortResult>()
                {
                    OutCode = -1,
                    OutMessage = "参数错误",
                };
            }

            DateTime startTime = System.DateTime.Now;
            int loopCount = 0;
            int exchangeCount = 0;

            ES.Node p = list.Next;
            while (p.Next != null)
            {
                ES.Node s = list;
                while (true)
                {
                    loopCount++;
                    if (s.Next == null)
                        break;
                    if (s != p)
                    {
                        if (s.Next.Num < p.Next.Num)
                        {
                            exchangeCount++;
                            ES.Node t = p.Next;
                            p.Next = t.Next;
                            t.Next = s.Next;
                            s.Next = t;
                            break;
                        }
                    }
                    else
                    {
                        p = p.Next;
                    }
                    s = s.Next;
                }
            }
            DateTime endTime = System.DateTime.Now;
            string strDuration = ((endTime - startTime).TotalMilliseconds).ToString();
            return new En.ExecultResult<En.SortResult>()
            {
                OutCode = 1,
                OutMessage = "成功",
                Data = new En.SortResult()
                {
                    LoopCount = loopCount,
                    ExChangCount = exchangeCount,
                    Duration = strDuration,
                    StartT = startTime.ToString("HH:mm:ss ffff"),
                    EndT = endTime.ToString("HH:mm:ss ffff")
                }
            };
        }

        /// <summary>
        /// 简单冒泡排序，其实是简单的两两交换
        /// </summary>
        /// <returns></returns>
        public static En.ExecultResult<En.SortResult> SimpleBubbleSort(ES.Node list)
        {

            if (BaseOperate.Length(list) < 2)
            {
                return new En.ExecultResult<En.SortResult>()
                {
                    OutCode = -1,
                    OutMessage = "参数错误",
                };
            }

            DateTime startTime = System.DateTime.Now;
            int loopCount = 0;
            int exchangeCount = 0;
            ES.Node p = list;
            while (p.Next != null)
            {
                ES.Node q = p.Next;
                while (q.Next != null)
                {
                    loopCount++;
                    if (p.Next.Num > q.Next.Num)
                    {
                        exchangeCount++;
                        ES.Node n = p.Next;
                        ES.Node m = q.Next;

                        p.Next = n.Next;
                        n.Next = m.Next;
                        m.Next = n;

                        if (n != q)
                        {
                            q.Next = m.Next;
                            m.Next = p.Next;
                            p.Next = m;
                        }
                        else
                        {
                            q = p.Next;
                        }
                    }
                    q = q.Next;
                }
                p = p.Next;
            }
            DateTime endTime = System.DateTime.Now;
            string strDuration = ((endTime - startTime).TotalMilliseconds).ToString();
            return new En.ExecultResult<En.SortResult>()
            {
                OutCode = 1,
                OutMessage = "成功",
                Data = new En.SortResult()
                {
                    LoopCount = loopCount,
                    ExChangCount = exchangeCount,
                    Duration = strDuration,
                    StartT = startTime.ToString("HH:mm:ss ffff"),
                    EndT = endTime.ToString("HH:mm:ss ffff")
                }
            };
        }

        public static En.ExecultResult<En.SortResult> BubbleSort(ES.Node list)
        {

            if (BaseOperate.Length(list) < 2)
            {
                return new En.ExecultResult<En.SortResult>()
                {
                    OutCode = -1,
                    OutMessage = "参数错误",
                };
            }

            DateTime startTime = System.DateTime.Now;
            int loopCount = 0;
            int exchangeCount = 0;
            int num = BaseOperate.Length(list);
            ES.Node e = null;
            while (num-- > 1)
            {
                ES.Node p = list;
                ES.Node q = p.Next;
                while (q.Next != e)
                {
                    loopCount++;
                    if (p.Next.Num > q.Next.Num)
                    {
                        exchangeCount++;
                        ES.Node n = q.Next;
                        q.Next = n.Next;

                        n.Next = p.Next;
                        p.Next = n;
                    }

                    p = p.Next;
                    q = p.Next;
                }
                e = q;
            }
            DateTime endTime = System.DateTime.Now;
            string strDuration = ((endTime - startTime).TotalMilliseconds).ToString();
            return new En.ExecultResult<En.SortResult>()
            {
                OutCode = 1,
                OutMessage = "成功",
                Data = new En.SortResult()
                {
                    LoopCount = loopCount,
                    ExChangCount = exchangeCount,
                    Duration = strDuration,
                    StartT = startTime.ToString("HH:mm:ss ffff"),
                    EndT = endTime.ToString("HH:mm:ss ffff")
                }
            };
        }

        public static En.ExecultResult<En.SortResult> OptimizeBubbleSort(ES.Node list)
        {
            if (BaseOperate.Length(list) < 2)
            {
                return new En.ExecultResult<En.SortResult>()
                {
                    OutCode = -1,
                    OutMessage = "参数错误",
                };
            }

            DateTime startTime = System.DateTime.Now;
            int loopCount = 0;
            int exchangeCount = 0;

            int num = BaseOperate.Length(list);
            ES.Node e = null;
            while (num-- > 1)
            {
                Boolean flag = false;
                ES.Node p = list;
                ES.Node q = p.Next;
                while (q.Next != e)
                {
                    loopCount++;
                    if (p.Next.Num > q.Next.Num)
                    {
                        exchangeCount++;
                        ES.Node n = q.Next;
                        q.Next = n.Next;

                        n.Next = p.Next;
                        p.Next = n;

                        flag = true;
                    }

                    p = p.Next;
                    q = p.Next;
                }
                if (flag == false)
                {
                    break;
                }
                e = q;
            } DateTime endTime = System.DateTime.Now;
            string strDuration = ((endTime - startTime).TotalMilliseconds).ToString();
            return new En.ExecultResult<En.SortResult>()
            {
                OutCode = 1,
                OutMessage = "成功",
                Data = new En.SortResult()
                {
                    LoopCount = loopCount,
                    ExChangCount = exchangeCount,
                    Duration = strDuration,
                    StartT = startTime.ToString("HH:mm:ss ffff"),
                    EndT = endTime.ToString("HH:mm:ss ffff")
                }
            };
        }
    }
}
